package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckGroupException;
import com.itheima.pojo.CheckGroup;
import com.itheima.vo.CheckGroupVo;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    /**
     * map & pojo 需要加@RequestBody
     * 基本类型 & 数组 & MultipartFile 只要保持页面的参数名称和controller方法形参一致就不用加@RequestParam
     * List 不管名字一不一样 必须加@RequestParam
     *
     * @return
     */
    @Reference
    private CheckGroupService checkGroupService;


    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.add(checkGroup);
            return Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return checkGroupService.findPage(queryPageBean);

    }

    @RequestMapping("/findDetailForUpdate")
    public Result findDetailForUpdate(Integer id) {
        CheckGroupVo checkGroupVo = checkGroupService.findDetailForUpdate(id);
        return Result.success("获取详情成功", checkGroupVo);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.edit(checkGroup);
            return Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkGroupService.delete(id);
            return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (CheckGroupException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
}

